//
//  UPTextField.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/17.
//

#import "UPTextField.h"
#import <Masonry/Masonry.h>

@interface UPTextField () <UITextFieldDelegate>
@property (nonatomic, assign) NSInteger maxLength;
@property (nonatomic, strong) UIView *divider;
@end

@implementation UPTextField

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
        [self setupConstraints];
    }
    return self;
}

- (void)setupSubviews {
    self.backgroundColor = [UIColor whiteColor];
    self.layer.borderWidth = 2.0;
    self.layer.borderColor = [UIColor colorWithHexString:@"#000000"].CGColor;
    self.layer.cornerRadius = 8.0;
    
    // 前缀标签
    _prefixLabel = [[UILabel alloc] init];
    _prefixLabel.font = [UIFont boldSystemFontOfSize:17];
    _prefixLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_prefixLabel];
    
    // 分割线
    _divider = [[UIView alloc] init];
    _divider.backgroundColor = [UIColor clearColor];
    [self addSubview:_divider];
    
    // 输入框
    _textField = [[UITextField alloc] init];
    _textField.font = [UIFont systemFontOfSize:15];
    _textField.delegate = self;
    [self addSubview:_textField];
}

- (void)setupConstraints {
    // 前缀标签约束
    [_prefixLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10);
        make.centerY.equalTo(self);
        make.width.mas_equalTo(40).priorityHigh(); // 默认宽度40
    }];
    
    // 分割线约束
    [_divider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_prefixLabel.mas_right).offset(5);
        make.centerY.equalTo(self);
        make.width.mas_equalTo(1);
        make.height.equalTo(self).multipliedBy(0.6); // 高度的60%
    }];
    
    // 输入框约束
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_divider.mas_right).offset(10);
        make.right.equalTo(self).offset(-10);
        make.top.bottom.equalTo(self);
    }];
}

- (void)configureWithPrefix:(NSString *)prefix
              prefixHidden:(BOOL)hidden
                maxLength:(NSInteger)maxLength
               placeholder:(NSString *)placeholder
              keyboardType:(UIKeyboardType)keyboardType {
    _prefixLabel.text = prefix;
    _prefixLabel.hidden = hidden;
    _maxLength = maxLength;
    _textField.placeholder = placeholder;
    _textField.keyboardType = keyboardType;
    
    // 根据是否隐藏前缀更新布局
    if (hidden) {
        [_textField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.right.equalTo(self).offset(-10);
            make.top.bottom.equalTo(self);
        }];
        _divider.hidden = YES;
    } else {
        [_textField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_divider.mas_right).offset(10);
            make.right.equalTo(self).offset(-10);
            make.top.bottom.equalTo(self);
        }];
        _divider.hidden = NO;
    }
}

- (void)updatePrefix: (NSString *)prefix {
    _prefixLabel.text = prefix;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (_maxLength <= 0) return YES;
    
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    return newString.length <= _maxLength;
}

@end
